class Lrc extends egret.DisplayObjectContainer
{
    public lrcArray = [];
    public curIndex = 0;
    public isChange = false;
    public lrcLayer: egret.DisplayObjectContainer;
    public fxLayer: egret.DisplayObjectContainer;
    public soundInfo =
    [
        {
            url: "http://www.windspirit.cn/weixin/games/resource/bgMusic.mp3",
            lrc: "resource/lrc.lrc"
        }
    ];
    public constructor()
    {
        super();
        this.width = 480;
        this.height = 50;

        this.lrcLayer = new egret.DisplayObjectContainer;
        this.lrcLayer.anchorX = 0.5;
        this.addChild(this.lrcLayer);

        this.fxLayer = new egret.DisplayObjectContainer;
        this.fxLayer.anchorX = 0.5;
        this.addChild(this.fxLayer);

        RES.getResByUrl(this.soundInfo[0].url,this.soundLoadComplete,this,RES.ResourceItem.TYPE_SOUND);
        RES.getResByUrl(this.soundInfo[0].lrc,this.lrcLoadComplete,this,RES.ResourceItem.TYPE_TEXT);
    }
    public getCb(e)
    {
        console.log(e.target.data);
    }
    public soundLoadComplete(sound: egret.Sound)
    {
        WindSpirit.System.curMusic = sound;
        sound.play(true);
        sound.addEventListener(egret.SoundEvent.SOUND_COMPLETE,this.soundEnd,this);
        this.addEventListener(egret.Event.ENTER_FRAME,this.update,this);
    }
    public soundEnd()
    {
        this.curIndex = 0;
    }
    public lrcLoadComplete(text)
    {
        this.parseLrc(text);
    }
    public update(e: egret.Event): void
    {
        var currentTime = WindSpirit.System.curMusic.position;
        var lrc = this.lrcArray[this.curIndex];
        if(this.curIndex < this.lrcArray.length - 1)
        {
            var nextLrc = this.lrcArray[this.curIndex + 1];
            if(currentTime >= lrc.time && currentTime < nextLrc.time)
            {
                this.isChange = false;
            }
            else
            {
                this.isChange = true;
                this.curIndex++;
                this.addLrcRow();
            }
        }
    }
    public addLrc(time: number,lrcstr: string): void
    {
        this.lrcArray.push({ "time": time,"lrcstr": lrcstr });
    }
    public parseLrc(lrcstr: string): void
    {
        var lrclines = lrcstr.split('\n');
        for(var i = 0;i < lrclines.length;i++)
        {
            var timeandlrc = lrclines[i].split(']');
            for(var j = 0;j < timeandlrc.length - 1;j++)
            {
                var timetemp = timeandlrc[j].match(/(\d+)\:(\d+)((\.|\:)(\d+))?/);

                if(timetemp && !/^\s*$/.test(timeandlrc[timeandlrc.length - 1]))
                {
                    var time = ((+timetemp[1] || 0) * 60 + (+timetemp[2] || 0)) * 1000 + (+timetemp[4] || 0) * 10;
                    var lrcstr = timeandlrc[timeandlrc.length - 1];
                    this.addLrc(time,lrcstr);
                }
            }
        }
    }
    public addLrcRow(): void
    {
        //WindSpirit.TransformBackground.getInstance().changeBg(null);
        this.lrcLayer.removeChildren();
        this.fxLayer.removeChildren();
        var lrc = this.lrcArray[this.curIndex].lrcstr;
        var len1 = lrc.length;
        for(var col = 0;col < len1;col++)
        {
            var label = new LrcLabel(lrc[col]);
            label.x = 30 * col;
            this.lrcLayer.addChild(label);
        }
        this.actionEffect(this.lrcLayer);
    }
    public actionEffect(container: egret.DisplayObject): void
    {
        var effectType = (Math.random() * 5) >> 0;
        var time = 1000;
        container.scaleX = container.scaleY = 5;
        egret.Tween.get(container).to({ scaleX: 1,scaleY: 1 },time,egret.Ease.backOut);
        switch(effectType)
        {
            case 0:
                EffectUtils.leftSlidingIn(container,time);
                break;
            case 1:
                EffectUtils.rightSlidingIn(container,time);
                break;
            case 2:
                EffectUtils.topSlidingIn(container,time);
                break;
            case 3:
                EffectUtils.bottomSlidingIn(container,time);
                break;
            case 4:
                EffectUtils.rotateLrc(container,time);
                break;
        }
        egret.Tween.get(container).wait(time).call(this.actionContainer.bind(this));
    }
    public actionContainer()
    {
        var config = new WindSpirit.FrameConfig;
        config.name = "blood_effect.zzz";
        config.frameRate = 10;
        config.frameNum = 10;
        var effect = new WindSpirit.FramePlay(config);
        effect.anchorX = effect.anchorY = 0.5;
        effect.scaleX = effect.scaleY = 0.3;
        effect.play();
        egret.Tween.get(effect).to({ x: this.lrcLayer.width },1000);
        this.fxLayer.addChild(effect);
    }
}